package com.example.bysj_sb.controller;

import org.apache.shiro.ShiroException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;


@ControllerAdvice
public class MyExceptionController {
    //private static final Logger logger= LoggerFactory.getLogger(MyExceptionController.class);
    public static final String DEFAULT_ERROR_VIEW = "error";
    public static final String DEFAULT_401_VIEW = "401";
    @ExceptionHandler(ShiroException.class)
    public final ModelAndView handleShiroExceptions(Exception ex, HttpServletRequest request){
        //  logger.error(ex.getMessage());
        ModelAndView modelAndView = new ModelAndView();
        //将异常信息设置入modelAndView
        modelAndView.addObject("msg", ex);
        modelAndView.addObject("url", request.getRequestURL());
        modelAndView.setViewName(DEFAULT_401_VIEW);
        //返回ModelAndView
        return modelAndView;
    }
    @ExceptionHandler(Exception.class)
    public final ModelAndView handleAllExceptions(Exception ex, HttpServletRequest request){
        //  logger.error(ex.getMessage());
        ModelAndView modelAndView = new ModelAndView();
        //将异常信息设置入modelAndView
        modelAndView.addObject("msg", ex);
        modelAndView.addObject("url", request.getRequestURL());
        modelAndView.setViewName(DEFAULT_ERROR_VIEW);
        //返回ModelAndView
        return modelAndView;
    }
}

